<?php
include_once('./Collection.php.inc');

class FileParser {

	//REGEXP-----------------
	//For collection parsing
	public $pRid;
	public $pTitle;
	public $pAbstract;
	public $pLinks;
	//For query parsing
	public $pQ;
	public $pRelevant;

	function __construct() {
		//INIT REGEXP-----------------
		$this->pRid = '/^<id>(.*)<\/id>/';
		$this->pTitle = '/^<title>(.*)<\/title>/';
		$this->pAbstract = '/^<abstract>(.*)<\/abstract>/';
		$this->pLinks = '/^<links>(.*)<\/links>/';

		$this->pQ = '/^<q>(.*)<\/q>/';
		$this->pRelevant = '/^<relevant>(.*)<\/relevant>/';
		//end INITREGEXP-------------
	}
	public function saveConcepts2($matrix, $coll){
		$rows=count($matrix);
		$cols=count($matrix[0]);
		$firstline = "Concepts, rows: $rows, columns: $cols\n";
		$data = "";
		$concepts=array();
		for($i=0;$i<$rows;$i++){
			$concept=array();
			for($j=0;$j<$cols;$j++){
				$val=$matrix[$i][$j];
				$concept[$coll->mapTermsInv[$j]]=$val;
			}	
			arsort($concept);
			foreach ($concept as $term => $val){
				$data.=$term.":".$english_format_number = number_format($val, 6, '.', '').", ";
			}
			$concepts[]=$concept;
			$data .= "\n";
		}
		
		
		file_put_contents("./svddata/concepts2.txt", $firstline.$data);
	}
	
	public function saveConcepts($matrix, $coll){
		$rows=count($matrix);
		$cols=count($matrix[0]);
		$firstline = "Concepts (%), rows: $rows, columns: $cols\n";
		$data = "";
		$concepts=array();
		for($i=0;$i<$rows;$i++){
			$concept=array();
			for($j=0;$j<$cols;$j++){
				$tmp=$matrix[$i][$j];
				$concept[$coll->mapTermsInv[$j]]=$tmp;
			}
			$minConceptValue = min($concept);
			for($j=0;$j<$cols;$j++){
				$concept[$coll->mapTermsInv[$j]] += $minConceptValue;
			}
			$totalSum = array_sum($concept);
			arsort($concept);
			foreach ($concept as $term => $val){
				$data.=$term.":".$english_format_number = number_format(($val/$totalSum)*100, 2, '.', '')."\t ";
			}
			$concepts[]=$concept;
			$data .= "\n";
		}
		
		
		file_put_contents("./svddata/concepts.txt", $firstline.$data);
	}

	public function writeInSVD($collection, $filename){
		$collection->mapDocsTerms2numbers();
		//$collection->mapTerms
		//$collection mapTermsInv
		
		//Rows Cols nonzerovals
		$rows=count($collection->docs);
		$cols=count($collection->mapTerms);
		$nonzeroValues=0;
		$allArrays=array();
		$data="";
		foreach($collection->docs as $doc){
			$docData=array();
			foreach($doc->weights as $term => $weight){
				if(isset($collection->mapTerms[$term])){
					$index=$collection->mapTerms[$term];
					$docData[$index]=$weight;
					$nonzeroValues++;
				}
			}
			ksort($docData);
			$data.=count($docData)."\n";
			foreach($docData as $index => $weight){
				$data.=$index." ".$weight."\n";
			}
		}
		$firstline=$rows." ".$cols." ".$nonzeroValues."\n";
		/*$tmp=array();
		$tmp=array_keys($docData);
		$firstline=$rows." ".end($tmp)." ".$nonzeroValues."\r\n";
		*/
		file_put_contents("./svddata/".$filename, $firstline.$data);
	}
	/*
	 * <-- ParseCollection -->
	 *
	 * return: Collection
	 */
	public function parseCollection($collection, $filePath, $label){
		//$lines = file('./data/Collection.xml',FILE_IGNORE_NEW_LINES);
		$lines = file($filePath,FILE_IGNORE_NEW_LINES);	
		$counter=0;
		foreach ($lines as $line_num => $line) {
			if (preg_match($this->pRid, $line, $matches)) {
				$rid = $matches[1];
				$d='';
			} elseif (preg_match($this->pTitle, $line, $matches)) {
				$title = $matches[1];
			}elseif(preg_match($this->pAbstract, $line, $matches)) {
				$abstract = $matches[1];
				//echo("ADDING DOCUMENT:\r\n ID: ".$rid."\r\nTITLE: ".$title."\r\nABSTRACT: ".$abstract."\r\n\r\n");
				$d = $collection->addDoc($rid, $title, $abstract, $label); #$d är ett dokument objekt
				$counter++;
			}
		}
		//echo $counter." documents was added\r\n";
	}


	/*
	 * <-- ParseQuieries -->
	 *
	 * return: Associative Array :: key = String query, val = Array<String> idsForRelevantDocsOfTheQuery
	 */
	public function parseQuieries(){
		$query_lines = file('./data/cran_queryRelevance.xml',FILE_IGNORE_NEW_LINES);
		//$query_lines = file('./data/queryRelevance.xml',FILE_IGNORE_NEW_LINES);

		$queries = array();
		#All the quieries are in a file quaryRelevance.xml, this file conations information about what the query is and it also gives a list of document ids that represents the relevant documents for the query
		foreach ($query_lines as $line_num => $line) {
			if(preg_match($this->pQ, $line, $matches)){//Query string line
				$query = $matches[1];
				//echo("Query ".$query."\r\n");
			}else if(preg_match($this->pRelevant, $line, $matches)){//The line where the relevant document ids are listed
				$relevant_string = $matches[1];
				//echo("Relevant string: ".$relevant_string."\r\n");
				$relevants=explode(", ", $relevant_string);
				//echo("Exploded: ".$relevants[0]." ".$relevants[1]." ".$relevants[2]."\r\n\r\n");
				$queries[$query]=$relevants;
			}
		}
		return $queries;
	}

	/*
	 * <-- ParsePageRank -->
	 *
	 * return: Associative Array :: key = String docID, val = Float pagerank
	 */
	public function parsePageRank(){
		$lines = file('./data/pagerank.txt',FILE_IGNORE_NEW_LINES);
		$pagerank=array();
		foreach ($lines as $line_num => $line) {
			$tmp = explode(";", $line);
			$pagerank[$tmp[0]]=doubleval($tmp[1]);
		}
		return $pagerank;
	}
	public function parseStopwords($stopwords){
		$lines = file('./data/stopwords.txt',FILE_IGNORE_NEW_LINES);
		foreach ($lines as $line_num => $line) {
			$stopwords[$line] = 0; 
		}
		return $stopwords;
	}
}
?>